package org.an.demo.test;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import com.nineoldandroids.view.ViewHelper;
import org.an.demo.MyApplication;
import org.an.demo.R;
import org.an.ku.base.BaseActivity;

public class AnimActivity extends BaseActivity {

  private ImageView mImageView;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_anim);

    mImageView = (ImageView) findViewById(R.id.imageView);
    View inflate =
        LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.item_opportunity, null);
    Context context = inflate.getContext();
  }

  @Override public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
      float rawX = event.getRawX();
      int measuredWidth = mImageView.getMeasuredWidth();
      // 这里只是demo,ImageView是全屏的，所以直接简写
      float scale = 1 - rawX / measuredWidth; // 1-0
      if (scale >= 0.7) {
        // 0.8基数，0.2变化速率，x+y=1
        float rightScale = 0.5f + scale * 0.5f;
        // 大小从1-0，缩放默认是以中心点进行的，会影响位移，要设置为以左边界
        ViewHelper.setPivotX(mImageView, 0);
        ViewHelper.setPivotY(mImageView, mImageView.getMeasuredHeight() / 2);
        // 需要等比缩放
        ViewHelper.setScaleX(mImageView, rightScale);
        ViewHelper.setScaleY(mImageView, rightScale);

        // 如果x是中点缩放，则把水平方向缩放引起的水平偏移算计去
        //float scaleOffset = measuredWidth / 2.0f * (1 - rightScale);
        //ViewHelper.setTranslationX(mImageView, measuredWidth * (1 - scale) - scaleOffset);
        float scaleOffset = 0;
        ViewHelper.setTranslationX(mImageView, measuredWidth * (1 - scale) - scaleOffset);
      }
    }
    return super.onTouchEvent(event);
  }
}
